/**
 * 
 */
package com.luck.pf.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipFile;

public class ZipUtil {
	/**
	 * 
	 * @param fileNames
	 *            包括目录path
	 * @param zipFileName
	 *            包括目录path
	 * @return
	 */
	public static synchronized boolean zipFile(List<String> fileNames,
			String zipFileName) throws Exception{
		FileInputStream fis;
		File zipFile = new File(zipFileName);
		File file;
		try {
			ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
			//for (int i = 0; i < fileNames.size(); i++) {
			for(String fileName:fileNames){
				int len;
				file = new File(fileName);
				fis = new FileInputStream(file);
				zos.putNextEntry(new ZipEntry(fileName));
				while ((len = fis.read()) != -1) {
					zos.write(len);
				}
				fis.close();
				file.delete();
			}
			zos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static synchronized boolean zipFile(String fileName,String zipFileName) throws Exception {
		File file = new File(fileName);
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		byte[] buf = new byte[1024];
		int len;
		FileOutputStream fos = new FileOutputStream(new File(zipFileName));
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		ZipOutputStream zos = new ZipOutputStream(bos);// 压缩包
		ZipEntry ze = new ZipEntry(file.getName());// 这是压缩包名里的文件名
		zos.putNextEntry(ze);// 写入新的 ZIP 文件条目并将流定位到条目数据的开始处
		while ((len = bis.read(buf)) != -1) {
			zos.write(buf, 0, len);
			zos.flush();
		}
		bis.close();
		zos.close();
		return true;
	}

	/**
	  * @param zipFileName 指定压缩文件
	  * @param destDir 指定解压目录
	  * @throws Exception
	  */
	 public static void unzip(String zipFileName, String destDir)
	   throws Exception {
	  try {
	   ZipFile zipFile = new ZipFile(zipFileName);
	   Enumeration<?> e = zipFile.getEntries();
	   org.apache.tools.zip.ZipEntry zipEntry = null;
	   File fD = new File(destDir);
	   if(!fD.exists()){
	    fD.mkdir();
	   }
	   while (e.hasMoreElements()) {
	    zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
	    String entryName = zipEntry.getName();
	    String names[] = entryName.split("/");
	    int length = names.length;
	    String path = destDir;
	    for (int v = 0; v < length; v++) {
	     if (v < length - 1) {
	      path += "/" + names[v];
	      new File(path).mkdir();
	     } else { 
	      if (entryName.endsWith("/")) { 
	       new File(destDir + "/" + entryName).mkdir();
	      } else {
	       InputStream in = zipFile.getInputStream(zipEntry);
	       OutputStream os = new FileOutputStream(new File(
	         destDir + "/" + entryName));
	       byte[] buf = new byte[1024];
	       int len;
	       while ((len = in.read(buf)) > 0) {
	        os.write(buf, 0, len);
	       }
	       in.close();
	       os.close();
	      }
	     }
	    }
	   }
	   zipFile.close();

	  } catch (Exception ex) {
	   ex.printStackTrace();
	  }
	 }
	 
	 /**
	  * @param targetZip 目标ZIP
	  * @param sourceFile 源文件
	  */
	 public static void zip(String targetZip, String sourceFile){
	  Project prj = new Project();
	  Zip zip = new Zip();
	  zip.setProject(prj);
	  zip.setDestFile(new File(targetZip));
	  FileSet fileSet = new FileSet();
	  fileSet.setProject(prj);
	  fileSet.setDir(new File(sourceFile));
	  // fileSet.setIncludes("**/*.txt");
	  zip.addFileset(fileSet);
	  zip.execute();
	 }

	 public static void main(String[] args) {

	  // ============
	  try {
	   zip("d:/tempsrc.zip", "d:/ziptest");
	   unzip("d:/tempsrc.zip", "d:/resultZip");
	  } catch (Exception e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	  }
	 }
}
